﻿/**********************************************************\
*  TinyLib - Camera                                        *
*                                                          *
*                                                          *
* Last edited: Jens Berglind  (10/07/2013)                 *
\**********************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using AALib.Input;

namespace AALib.Camera
{
    public enum CameraMode
    {
        Free,
        Center,
        ZoomIn,
        ZoomOut
    }

    public enum PlayerDistance
    {
        Distance1 = 500,
        Distance2 = 1000,
        Distance3 = 1500,
        Distance4 = 2000
    };

    public class Camera : GameComponent
    {
        #region Fields

        protected float m_fZoom;
        public Matrix m_transform;
        public Vector2 m_CamPosition;
        public Vector2 m_CamPosition2;
        protected float m_fRotation;
        public float m_fMaxZoom;
        public float m_fMinZoom;
        private IniFile m_config;
        Keys m_camUp, m_camDown, m_camRight, m_camLeft;
        public Viewport m_viewport;
        public Vector2 m_camDirection = new Vector2(0.0f, 0.0f);

        #endregion

        #region Properties

        public float FloatZoom
        {
            get { return m_fZoom; }
            set
            {
                m_fZoom = value;

                if (m_fZoom < m_fMinZoom)
                {
                    m_fZoom = m_fMinZoom;
                }

                else if (m_fZoom > m_fMaxZoom)
                {
                    m_fZoom = m_fMaxZoom;
                }
            }
        }

        public float FloatRotation
        {
            get { return m_fRotation; }
            set { m_fRotation = value; }
        }

        public Vector2 CameraPosition
        {
            get { return m_CamPosition; }
            set { m_CamPosition = value; }
        }

        #endregion

        #region Inits

        public Camera(Game p_game, Viewport p_viewport)
            : base(p_game)
        {
            m_config = new IniFile("config.ini");
            m_config.Parse();

            //fMaxZoom = float.Parse(config.GetValue("Camera", "MaxZoom"));
            //fMinZoom = float.Parse(config.GetValue("Camera", "MinZoom"));

            this.m_viewport = p_viewport;

            m_fZoom = 1.0f;
            m_fRotation = 0.0f;
            m_CamPosition = new Vector2(
                                        p_viewport.Width * 0.5f,
                                        p_viewport.Height * 0.5f
                                      );
            m_CamPosition2 = new Vector2(
                                        p_viewport.Width * 0.5f,
                                        p_viewport.Height * 0.5f
                                      );

            // viewport = new Viewport(0, 0, 
        }

        #endregion

        #region Methods

        public void Update(GameTime p_gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();
            float fDeltaTime = (float)p_gameTime.ElapsedGameTime.Milliseconds;
        }

        public void Move(Vector2 p_newCamPos)
        {
            m_CamPosition += p_newCamPos;
        }


        public Matrix GetTransformation(GraphicsDevice p_graphicsDevice)
        {
            m_transform = Matrix.CreateTranslation
                        (
                            new Vector3(-m_CamPosition.X, -m_CamPosition.Y, 0)) *
                            Matrix.CreateRotationZ(FloatRotation) *
                            Matrix.CreateScale(new Vector3(FloatZoom, FloatZoom, 1)) *
                            Matrix.CreateTranslation(new Vector3(m_viewport.Width * 0.5f,
                                                                 m_viewport.Height * 0.5f,
                                                                 0));

            return m_transform;
        }

        #endregion
    }
}